﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Xna.Framework;

namespace Athanor.Code
{
    class Level
    {
        List<Mesh> m_Meshes = new List<Mesh>();

        public bool Init(string _szFileName)
        {
            ParseLevel(_szFileName);

            return true;
        }

        void ParseLevel(string _szFileName)
        {
            String dir = "E:\\Atha-nor\\Programming\\Content\\test\\test.xml";
            XmlTextReader reader = new XmlTextReader(dir);
            reader.Read();

            XmlDocument doc = new XmlDocument();
            doc.Load(reader);

            XmlElement root = (XmlElement)doc.GetElementsByTagName("Root")[0];

            for (int i = 0; i < root.ChildNodes.Count; i++)
            {
                XmlNode node = root.ChildNodes[i];

                for (int j = 0; j < node.ChildNodes.Count; j++)
                {
                    Mesh newMesh = new Mesh();

                    XmlNode obj_node = node.ChildNodes[j];

                    string name = obj_node.Attributes["name"].InnerXml;
                    newMesh.Load(name);

                    string[] szVar = obj_node.Attributes["position"].InnerXml.Split(' ');
                    float x = Convert.ToSingle(szVar[0]);
                    float y = Convert.ToSingle(szVar[1]);
                    float z = Convert.ToSingle(szVar[2]);

                    newMesh.SetPosition(new Vector3(x, y, z));

                    szVar = obj_node.Attributes["rotation"].InnerXml.Split(' ');
                    x = Convert.ToSingle(szVar[0]);
                    y = Convert.ToSingle(szVar[1]);
                    z = Convert.ToSingle(szVar[2]);

                    newMesh.SetRotation(new Vector3(x, y, z));

                    m_Meshes.Add(newMesh);
                }
            }
        }

        public void Draw()
        {
            for (int i = 0; i < m_Meshes.Count; i++)
			{
			    m_Meshes[i].Draw();
			}
        }
    }
}
